package com.example.demo;

import com.lowagie.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;


public class HtmlToPdf {

    public static void main(String[] args) throws Exception {
        new HtmlToPdf().htmlToPdf("C:\\tempFiles\\5207406fc8394bb4b56ae13b999994a3\\test2003.html","C:\\tempFiles\\5207406fc8394bb4b56ae13b999994a3\\test2003.pdf");
    }

    public void htmlToPdf(String htmlPath, String pdfPath) throws Exception {
        String path = htmlPath;
        String destPath =pdfPath;
        ITextRenderer renderer = new ITextRenderer();
        OutputStream os = new FileOutputStream(destPath);
        renderer.setDocument(new File(path));
        ITextFontResolver resolver = renderer.getFontResolver();
        //添加字体，解决中文不显示的问题
//        resolver.addFont(resourcesDir + "/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        renderer.layout();
        renderer.createPDF(os);
        os.close();
    }
}
